import os,sys,commands


def pages2mb(pages):
  return int(float(pages) / 256)
def mb2pages(megs):
  return int(float(megs) * 256)
def bytes2mb(bytes):
  return int(float(bytes) / 1048576)
def mb2bytes(megs):
  return int(float(megs) * 1048576)
def kb2gb(kbs):
  return int(float(kbs) / 1048576)
def gb2kb(gbs):
  return int(float(gbs) * 1048576)
def inodes(m):
  soft = int(float(m) * 200000)
  hard = int(float(m) * 220000)
  return "%s:%s" % (soft, hard)
  

def nodes():
  nodes = vzlist()
  
  for node in nodes:
    node['user']          = get_user(node['hostname'])
    node['ip_addresses']  = get_conf_opt('ip_address', node['veid'])
    memory                = get_conf_opt('vmguarpages', node['veid'])
    node['memory']        = pages2mb(memory.split(':')[0])
    disk                  = get_conf_opt('diskspace', node['veid'])
    node['disk']          = kb2gb(disk.split(':')[0])
  return nodes

def node(node_id):
  nodes = vzlist()
  ret_val = {'veid': 'none'}
  
  for node in nodes:
    if node['veid'] == node_id or node['hostname'] == node_id:
      node['user']          = get_user(node['hostname'])
      node['ip_addresses']  = get_conf_opt('ip_address', node['veid'])
      memory                = get_conf_opt('vmguarpages', node['veid'])
      node['memory']        = pages2mb(memory.split(':')[0])
      disk                  = get_conf_opt('diskspace', node['veid'])
      node['disk']          = kb2gb(disk.split(':')[0])
      ret_val = node
  return ret_val

def print_node(node):
  if node['veid'] == 'none':
    return
  else:
    print "%20s : %s"     % ('Node Name',node['hostname'])
    print "%20s : %s"     % ('VEID', node['veid'])
    print "%20s : %s"     % ('Owning User', node['user'])
    print "%20s : %s MB"  % ('Guarenteed Memory', node['memory'])
    print "%20s : %s GB"  % ('Disk Space', node['disk'])
    print "%20s : %s"     % ('Running Status', node['status'])

def vzlist():
  orig = commands.getoutput('vzlist -a')
  ret_val = []
  
  for line in orig.split('\n'):
    node = {
                'veid': line.strip().split()[0],
               'nproc': line.strip().split()[1],
              'status': line.strip().split()[2],
              'pri_ip': line.strip().split()[3],
            'hostname': line.strip().split()[4]
           }
    if node['veid'] == 'VEID':
      continue
    else:
      ret_val.append(node)
  return ret_val

def get_user(node):
  ldapsearch = commands.getoutput('ldapsearch -h 172.16.0.12 -LLL -x -b "dc=cugnet,dc=local" host=' + node)
  ret_val = 'none'
  
  for line in ldapsearch.split('\n'):
    if line == "":
      continue
    else:
      values = line.strip().split()
      if values[0] == 'uid:':
        ret_val = values[1]
  return ret_val

def get_conf_opt(search_option, veid):
  config = open('/etc/vz/conf/' + veid + '.conf', 'r')
  ret_val = 'none'
  
  for line in config:
    if line.find('#') != -1 or line.find('=') == -1:
      continue    
    option = line.split('=')[0]
    value = line.split('=')[1].split('"')[1]
    
    if option == search_option.upper():
      ret_val = value
  return ret_val